﻿using System;
using System.Collections.Generic;
using Manso.cBot.Common;
namespace Manso.cBot.DefaultModules.Kos
{
    public class KosAddCommand : ICommand
    {
        private ListenToEnum _listenTo;
        private string _command;
        private string _help;
        private string _usage;
        private AccessControlLevel _accessLevel;

        public KosAddCommand()
        {
            _listenTo = ListenToEnum.Both;
            _command = "KosAdd";
            _help = "Add a target to the kos list";
            _usage = "kosadd character_name [comment]";
            _accessLevel = AccessControlLevel.Basic;
        }

        public ListenToEnum ListenTo
        {
            get { return _listenTo; }
        }

        public string Command
        {
            get { return _command; }
        }

        public string Help
        {
            get { return _help; }
        }

        public string Usage
        {
            get { return _usage; }
        }

        public AccessControlLevel AccessLevel
        {
            get { return _accessLevel; }
        }

        public void ExecuteCommand(string[] args, string executedBy, ListenToEnum listenTo)
        {
            if (args.Length <= 0 || args[0].Length <= 0)
            {
                cBotContext.Instance.SendPrivateMessage(executedBy, "Incorrect arguments. Try help for usage information");
                return;
            }

            List<String> auxComment = new List<string>();
            auxComment.AddRange(args);
            auxComment.RemoveAt(0);
            String comment = String.Join(" ", auxComment.ToArray());

            KosList kosList = new KosList();
            kosList.ReadXml("Kos/KosList.xml");
            KosList.AddedByRow addedByRow = kosList.AddedBy.NewAddedByRow();
            addedByRow.CharacterName = executedBy;
            addedByRow.Comment = comment;
            foreach(KosList.KosTargetRow targetRow in kosList.KosTarget)
            {                
                if (targetRow.CharacterName.ToLower().Equals(args[0].ToLower()))
                {
                    addedByRow.KosTargetRow = targetRow;
                    kosList.AddedBy.AddAddedByRow(addedByRow);
                    kosList.WriteXml("Kos/KosList.xml");
                    cBotContext.Instance.SendMessage("Kos target added succefully", executedBy, listenTo);
                    return;
                }
            }

            KosList.KosTargetRow newTargetRow = kosList.KosTarget.NewKosTargetRow();
            newTargetRow.CharacterName = args[0];
            addedByRow.KosTargetRow = newTargetRow;
            kosList.KosTarget.AddKosTargetRow(newTargetRow);
            kosList.AddedBy.AddAddedByRow(addedByRow);            
            kosList.WriteXml("Kos/KosList.xml");
            cBotContext.Instance.SendMessage("Kos target added succefully", executedBy, listenTo);
        }
    }
}